package com.ruoyi.project.propertySystem.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.TreeSelectInspectItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.propertySystem.domain.InspectItem;
import com.ruoyi.project.propertySystem.service.IInspectItemService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;


/**
 * 巡检项主Controller
 * 
 * @author wangshuo
 * @date 2021-01-18
 */
@RestController
@RequestMapping("/propertySystem/inspectItem")
public class InspectItemController extends BaseController
{
    @Autowired
    private IInspectItemService inspectItemService;

    /**
     * 查询巡检项主列表
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectItem inspectItem)
    {
        startPage();
        List<InspectItem> list = inspectItemService.selectInspectItemList(inspectItem);
        return getDataTable(list);
    }

    /**
     * 导出巡检项主列表
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:export')")
    @Log(title = "巡检项主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InspectItem inspectItem)
    {
        List<InspectItem> list = inspectItemService.selectInspectItemList(inspectItem);
        ExcelUtil<InspectItem> util = new ExcelUtil<InspectItem>(InspectItem.class);
        return util.exportExcel(list, "inspectItem");
    }

    /**
     * 获取巡检项主详细信息
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(inspectItemService.selectInspectItemById(id));
    }

    /**
     * 新增巡检项主
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:add')")
    @Log(title = "巡检项主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectItem inspectItem)
    {
        return toAjax(inspectItemService.insertInspectItem(inspectItem));
    }

    /**
     * 修改巡检项主
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:edit')")
    @Log(title = "巡检项主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectItem inspectItem)
    {
        inspectItem.setModifyBy(SecurityUtils.getUsername());
        inspectItem.setGmtModify(new Date());
        return toAjax(inspectItemService.updateInspectItem(inspectItem));
    }

    /**
     * 删除巡检项主
     */
    @PreAuthorize("@ss.hasPermi('propertySystem:inspectItem:remove')")
    @Log(title = "巡检项主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(inspectItemService.deleteInspectItemByIds(ids));
    }

    /**
     * 巡检分类巡检项树
     */
    @GetMapping(value = "/inspectClassifyItemTreeselect/{formworkId}")
    public AjaxResult inspectClassifyItemTreeselect(@PathVariable(value = "formworkId") Long formworkId)
    {
//        Long a;
//        if(!StringUtils.isBlank(formworkId)){
//            a= Long.valueOf(formworkId);
//        }else {
//            a= null;
//        }
        List<TreeSelectInspectItem> inspectItems = inspectItemService.selectInspectItemTree(formworkId);
        List<String> checkIds = inspectItemService.selectCheckItemByFormworkId(formworkId);
//        List<SysArea> areas = areaService.selectAreaList(new SysArea());
        AjaxResult ajax = AjaxResult.success();
//        ajax.put("checkedKeys", areaService.selectAreaListByRoleId(roleId));
//        ajax.put("checkedKeys", areaService.selectAreaAndStationListByRoleId(roleId));
//        ajax.put("areas", areaService.buildAreaTreeSelect(areas));
//        ajax.put("itemTree", areaService.buildAreaStationTreeSelect(areas));
        ajax.put("itemTree", inspectItems);
        ajax.put("checkedKeys", checkIds);
        return ajax;
    }
}
